//
//  VideoViewController.m
//  Brand
//
//  Created by Faker on 4/8/14.
//  Copyright (c) 2014 Faker. All rights reserved.
//

#import "VideoViewController.h"
#import "config.h"
#import "VideoDetailVC.h"
#import "VideoObj.h"
#import "Header.h"

@interface VideoViewController ()
@property (strong, nonatomic) IBOutlet UIView *navTopContentView;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;

@property (strong, nonatomic) CmsScrollView* cmsScrollView;
@property (strong, nonatomic) NSArray* videoArray;
@property (strong, nonatomic) NSArray* filmArray;
@property (strong, nonatomic) NSDictionary* bigPicDic;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (weak, nonatomic) IBOutlet UIView *filmView;

@property (strong, nonatomic) iCarousel* myIcarousel;
@property (strong, nonatomic) UIImageView* myImageView;

@end

@implementation VideoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self drawNavTopContentView:self.navTopContentView];
    [self setUp];
}

-(void)setUp
{
    
    self.myIcarousel = [[iCarousel alloc]initWithFrame:CGRectMake(0, 0, 300, 160)];
    self.myIcarousel.delegate = self;
    self.myIcarousel.dataSource = self;
    self.myIcarousel.decelerationRate=.2;
    self.myIcarousel.type=iCarouselTypeCoverFlow;

    
    self.myScrollView.contentHeight = self.myScrollView.height+1;
    
    self.cmsScrollView = [[CmsScrollView alloc]initWithFrame:self.contentView.bounds];
    
    self.cmsScrollView.dataSource = self;
    self.cmsScrollView.delegate = self;
    [self.contentView addSubview:self.cmsScrollView];
    [self.contentView sendSubviewToBack:self.cmsScrollView];
    self.contentView.hidden = YES;
   
    //[self.cmsScrollView reloadData];
    
    [self prepareGetData];
}

#pragma mark - 网络请求
-(void)prepareGetData
{
    [kApp displayWaitViewWithAllMaskView:NO];
    NSString* url = [NSString stringWithFormat:@"%@videoPage",API_BASE];
   
    [[AFGetData sharedInstance] startGet:url andResultIsJSON:YES andRequestCompletion:^(id responseObj, BOOL isSuccess) {
        if(isSuccess)
        {
            [self getDataSuccess:responseObj];
        }
        else{
            [self getDataError];
        }
    }];
}

-(void)getDataSuccess:(id)responseObj
{
    [kApp hideWaitView];
    
    
    NSDictionary* allDic = (NSDictionary*)responseObj;
    [UserDataBasic setObject:allDic forKey:LocalVideoData];
    [self handleDataWithDic:allDic];
    
}

-(void)getDataError
{
    [kApp hideWaitView];
    [PopTips tips:self.view Msg:NotifyNoNet];
    
    NSDictionary* allDic = [UserDataBasic getObjectForKey:LocalVideoData];
    if(!IsNull(allDic))
    {
        [self handleDataWithDic:allDic];
    }
    else{
        [self performSelector:@selector(goBack:) withObject:self afterDelay:0.7];
    }
}

-(void)handleDataWithDic:(NSDictionary*)allDic
{
    
    self.bigPicDic = [allDic objectForKey:@"bigPic"];
    self.videoArray =[allDic objectForKey:@"cartoon"];
    self.filmArray = [allDic objectForKey:@"movie"];
    self.titleLabel.text = [self.bigPicDic objectForKey:@"title"];
    [[AFGetData sharedInstance] initSDWebImage:[self.bigPicDic objectForKey:@"pic"] andTrueImageView:self.imageView andPlaceholderImage:nil];
    
    self.contentView.hidden = NO;
    [self.cmsScrollView reloadData];
    
    [self.filmView addSubview:self.myIcarousel];
    [self.myIcarousel reloadData];

}

#pragma mark - iCarousel methods
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return [self.filmArray count];
}

- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
    return 3;
}


- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
	//create new view if no view is available for recycling
	if (view == nil) //如果没有可以重用的
	{
        view = [[[NSBundle mainBundle] loadNibNamed:@"VideoCarouselCell" owner:self options:nil] objectAtIndex:0];
        
    }
    UILabel* titleLabel = (UILabel*)[view viewWithTag:200];
    UIImageView* imageView = (UIImageView*)[view viewWithTag:302];
    titleLabel.text = [[self.filmArray objectAtIndex:index] objectForKey:@"title"];
    [[AFGetData sharedInstance] initSDWebImage:[[self.filmArray objectAtIndex:index] objectForKey:@"pic"] andTrueImageView:imageView andPlaceholderImage:nil];
    view.layer.cornerRadius = 2;
	return view;
}

-(void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index{
    
    NSDictionary* dic = [self.filmArray objectAtIndex:index];
    
    VideoObj* videoObj = [[VideoObj alloc]init];
    videoObj.episodeId = [dic objectForKey:@"id"];
    videoObj.contentName = [dic objectForKey:@"title"];
    videoObj.isReadyShow = [[dic objectForKey:@"is_ok"] boolValue];
    videoObj.isNeedVIP = [[dic objectForKey:@"v_flag"] boolValue];
    VideoDetailVC* detailVC = [[VideoDetailVC alloc]initWithNibName:@"VideoDetailVC" bundle:nil];
    detailVC.videoObj = videoObj;
    
    if(index == carousel.currentItemIndex)
    {
        [self.navigationController pushViewController:detailVC animated:YES];
    }
    
}



-(void)carouselDidEndScrollingAnimation:(iCarousel *)carousel
{
    
    [self addGoldBorder];
}

-(void)addGoldBorder
{
    [self.myImageView removeFromSuperview];
    UIView* currentView = [self.myIcarousel currentItemView];
    
    NSArray* visibleViews = [self.myIcarousel visibleItemViews];
    
    for (UIView* cusView in visibleViews) {
        UIView* shadeView = (UIView*)[cusView viewWithTag:202];
        UIImageView* imageView = (UIImageView*)[cusView viewWithTag:303];
        if(cusView == currentView)
        {
            imageView.hidden = NO;
            shadeView.hidden = YES;
        }
        else{
            imageView.hidden = YES;
            shadeView.hidden = NO;
        }
    }
}



-(BOOL)carousel:(iCarousel *)carousel shouldSelectItemAtIndex:(NSInteger)index
{
    return YES;
}

//可不可以循环滚动
-(BOOL)carouselShouldWrap:(iCarousel *)carousel
{
    return YES;
}

//每个item的间隔宽度
- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
    //usually this should be slightly wider than the item views
    return 150; //每一项的宽度
}

-(CGFloat)carousel:(iCarousel *)carousel valueForTransformOption:(iCarouselTranformOption)option withDefault:(CGFloat)value
{
    return 0.48;
}



#pragma mark - CmsScrollViewDataSource
-(int)numberOfPages
{
    NSLog(@"count = %d",self.videoArray.count);
    return [self.videoArray count];
}

-(UIView*)pageAtIndex:(int)index
{
    UIView* tempView = [[[NSBundle mainBundle]loadNibNamed:@"VideoCell" owner:self options:nil]lastObject];
    UILabel* indexLable = (UILabel*)[tempView viewWithTag:200];
    UIImageView* imageView = (UIImageView*)[tempView viewWithTag:302];
    
    NSDictionary* dic = [self.videoArray objectAtIndex:index];
    indexLable.text = [dic objectForKey:@"title"];
    [[AFGetData sharedInstance] initSDWebImage:[dic objectForKey:@"pic"] andTrueImageView:imageView andPlaceholderImage:nil];
    
    return tempView;
}

-(void)cmsScrollView:(CmsScrollView *)cmsScrollView didSelectItemAtIndex:(int)index
{
   
    VideoDetailVC* detailVC = [[VideoDetailVC alloc]initWithNibName:@"VideoDetailVC" bundle:nil];
    NSDictionary* dic = [self.videoArray objectAtIndex:index];
    VideoObj* videoObj = [[VideoObj alloc]init];
    videoObj.episodeId = [dic objectForKey:@"id"];
    videoObj.contentName = [dic objectForKey:@"title"];
    videoObj.isReadyShow = [[dic objectForKey:@"is_ok"] boolValue];
    videoObj.isNeedVIP = [[dic objectForKey:@"v_flag"] boolValue];
    
    detailVC.videoObj = videoObj;
    if(videoObj.isReadyShow)
    {
        [self.navigationController pushViewController:detailVC animated:YES];
    }
    else{
        [PopTips tips:self.view Msg:NotifyVideoNotOpen];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - xib点击事件
- (IBAction)goBack:(id)sender {
    [[AFGetData sharedInstance] removeAllGetAndPostRequest];
    [kApp hideWaitView];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)playClick:(id)sender {
    VideoDetailVC* detailVC = [[VideoDetailVC alloc]initWithNibName:@"VideoDetailVC" bundle:nil];
    
    VideoObj* videoObj = [[VideoObj alloc]init];
    videoObj.episodeId = [self.bigPicDic objectForKey:@"id"];
    videoObj.contentName = [self.bigPicDic objectForKey:@"title"];
    detailVC.videoObj = videoObj;
    [self.navigationController pushViewController:detailVC animated:YES];
}


@end
